﻿using Company.Application.Share.Events;
using Company.Application.Share.Session;
using Company.Core.Enums;
using Company.Core.IOC;
using Company.Core.Language;
using Prism.Commands;
using Prism.Mvvm;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Company.Core.Helpers;
using Microsoft.Win32;
using System.IO;
using Company.Application.Share.Prism;
using Company.Application.Share.Config;
using Company.Core.Config;
using Company.Core.Cache;
using Company.UI.Controls;

namespace Company.Application.Login.ViewModels
{
    public class LoginViewModel : ReactiveObject
    {
        private readonly ILanguageManager _languageManager;
        private readonly ICacheManager _cacheManager;
        public LoginViewModel(ILanguageManager languageManager, ICacheManager cacheManager)
        {
            _languageManager = languageManager;
            _cacheManager = cacheManager;
            Languages = languageManager.Languages;
            LoadCache();  // 加载缓存
            _languageManager.Set(CurrentLanguage.Type); // 设置配置的语言
            LoginCommand = new DelegateCommand(DoLogin);
            SelectedLanguageCommand = new DelegateCommand(DoSelectedLanguage);

            // 自动登录
            Task.Delay(2000).ContinueWith(t =>
            {
                PrismProvider.Dispatcher.Invoke(() =>
                {
                    if (IsAutoLogin)
                    {
                        DoLogin();
                    }
                });

            });
        }

        [Reactive]
        public CurrentUser CurrentUser { get; set; } = new CurrentUser();

        [Reactive]
        public bool IsRemember { get; set; }

        [Reactive]
        public bool IsAutoLogin { get; set; }

        [Reactive]
        public List<LanguageModel> Languages { get; set; }

        [Reactive]
        public LanguageModel CurrentLanguage { get; set; }

        public ICommand LoginCommand { get; }

        public ICommand SelectedLanguageCommand { get; }

        private void DoSelectedLanguage()
        {
            _languageManager.Set(CurrentLanguage.Type);
        }

        private void DoLogin()
        {
            SaveCache(); // 保存缓存

            // TODO login
            if (string.IsNullOrEmpty(CurrentUser.UserName) || string.IsNullOrEmpty(CurrentUser.Password))
            {
                PopupBox.Show(_languageManager[LanguageKeys.UsernameOrPWDCannotBeEmpty]);
                return;
            }

            // 登录成功，发布消息
            PrismProvider.EventAggregator.GetEvent<LoginSuccessedEvent>().Publish(CurrentUser);
        }

        /// <summary>
        /// 加载缓存
        /// </summary>
        private void LoadCache()
        {
            CurrentUser = _cacheManager.Get<CurrentUser>(CacheKeys.User) ?? new CurrentUser();
            IsRemember = _cacheManager.Get<bool>(CacheKeys.IsRemember);
            IsAutoLogin = _cacheManager.Get<bool>(CacheKeys.IsAutoLogin);
            var configLanguage = _cacheManager.Get<LanguageModel?>(CacheKeys.Language) ?? _languageManager.Current;
            CurrentLanguage = Languages.FirstOrDefault(d => d.Type == configLanguage.Type);
        }

        /// <summary>
        /// 保存缓存
        /// </summary>
        private void SaveCache()
        {
            _cacheManager.Set(CacheKeys.IsRemember, IsRemember);
            _cacheManager.Set(CacheKeys.IsAutoLogin, IsAutoLogin);
            _cacheManager.Set(CacheKeys.Language, CurrentLanguage);
            if (IsRemember)
            {
                _cacheManager.Set(CacheKeys.User, CurrentUser);
            }
            else
            {
                _cacheManager.Delete(CacheKeys.User);
            }
        }
    }
}
